﻿using YR.Framework.Core;
using YR.Framework.MVVM;
using HZY.Framework.DependencyInjection;
using Mapster;

namespace SingleWpfProject
{
    public class ProjectManageViewModel: 
        CRUDViewModel<ProjectAddDto, ProjectDeleteDto, ProjectUpdateDto, ProjectPageQueryDto, ProjectPageQueryResultDto>,IScopedSelfDependency
    {
        private readonly ProjectAppService _projectAppService;

        public ProjectManageViewModel(ProjectAppService projectAppService)
        {
            _projectAppService = projectAppService;
        }

        public override bool BeforeAdd()
        {
            return true;
        }

        public override bool BeforeDelete()
        {
            return true;
        }

        public override bool BeforeUpdate()
        {
            return true;
        }

        public override async Task DoAddAsync()
        {
            AddDto.SetBaseCreateDto("ssss");

            BaseResult baseResult = await _projectAppService.AddAsync(AddDto);
            if (baseResult.Code == ResultCode.Success) 
                await LoadDataAsync();
        }

        public override async Task DoDeleteAsync()
        {
            BaseResult baseResult = await _projectAppService.DeleteAsync(new ProjectDeleteDto() { Id = SelectedItem.Id});
            if (baseResult.Code == ResultCode.Success)
                await LoadDataAsync();
        }

        public override async Task DoUpdateAsync()
        {
            UpdateDto = SelectedItem.Adapt<ProjectUpdateDto>();

            BaseResult baseResult = await _projectAppService.UpdateAsync(UpdateDto);
            if (baseResult.Code == ResultCode.Success)
                await LoadDataAsync();
        }

        public override async Task LoadDataAsync()
        {
            var result = await _projectAppService.QueryDataAsync(DataQueryDto);

            DataBindingHelper.UpdateObservableCollection(DataResults, result.Data);
        }
    }
}
